package mysql

import (
	"gorm.io/gorm"
	"user-good/productrpc/productrpc"
)

type Good struct {
	gorm.Model
	GoodName   string  `gorm:"type:varchar(30);not null"`
	GoodPrice  float64 `gorm:"type:decimal(10,2);not null"`
	GoodStatus string  `gorm:"type:varchar(30);not null"`
	UserID     int     `gorm:"type:int;not null"`
}

func (g *Good) CreateGood(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func GoodList(db *gorm.DB, id int64) []*productrpc.GoodList {
	var good []*productrpc.GoodList
	db.Debug().Model(&Good{}).
		Select("goods.*", "users.id").
		Joins("LEFT JOIN users ON goods.user_id = users.id").
		Where("goods.user_id = 1", id).
		Find(&good)
	return good
}
